/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.market.web.service;

import com.market.web.EJB.InventoryFacade;
import com.market.web.model.Inventory;
import java.math.BigDecimal;
import java.util.List;
import javax.ejb.EJB;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

/**
 *
 * @author hp
 */
@Path("/Inventory")
public class Source {
    @EJB
     InventoryFacade inventory;
    
    @GET   
    public JsonObject RetrieveInventory() {
        
        //System.out.println("herrrr");
        List<Inventory> lstInventory = inventory.findAll();
        JsonObjectBuilder itemsObject = Json.createObjectBuilder();
        JsonArrayBuilder items = Json.createArrayBuilder();
        
            for (Inventory inv : lstInventory) {
                JsonObjectBuilder item = Json.createObjectBuilder();
                item.add("pId", inv.getPid());
                item.add("pName", inv.getName());
                item.add("pPrice", inv.getPrice());
                item.add("pQty", inv.getQty());
                item.add("pImg", inv.getImageName());
                if(inv.getType()!=null)
                {
                    item.add("pType", inv.getType());
                }
                
                 if(inv.getDescription()!=null)
                {
                    item.add("pDesc", inv.getDescription());
                }

                items.add(item);
                itemsObject.add("items", items);

    }
       return( itemsObject.build());
       
    }
   
}
